package com.houweishi.module.leave.controller;

import com.houweishi.common.annotation.Log;
import com.houweishi.common.core.controller.BaseController;
import com.houweishi.common.core.domain.AjaxResult;
import com.houweishi.common.core.page.TableDataInfo;
import com.houweishi.common.enums.BusinessType;
import com.houweishi.common.utils.poi.ExcelUtil;
import com.houweishi.module.leave.domain.OaLeave;
import com.houweishi.module.leave.domain.OaLeaveApproval;
import com.houweishi.module.leave.service.IOaLeaveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 请假管理Controller
 * 
 * @author houweishi
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/module/leave")
public class OaLeaveController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(OaLeaveController.class);
    @Autowired
    private IOaLeaveService oaLeaveService;

    /**
     * 查询请假管理列表
     */
    @PreAuthorize("@ss.hasPermi('module:leave:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaLeaveApproval oaLeave)
    {
        startPage();
        List<OaLeaveApproval> list = oaLeaveService.selectOaLeaveApprovalList(oaLeave);
        return getDataTable(list);
    }

    /**
     * 导出请假管理列表
     */
    @PreAuthorize("@ss.hasPermi('module:leave:export')")
    @Log(title = "请假管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaLeave oaLeave)
    {
        List<OaLeave> list = oaLeaveService.selectOaLeaveList(oaLeave);
        ExcelUtil<OaLeave> util = new ExcelUtil<OaLeave>(OaLeave.class);
        util.exportExcel(response, list, "请假管理数据");
    }

    /**
     * 获取请假管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:leave:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(oaLeaveService.selectOaLeaveById(id));
    }

    /**
     * 新增请假管理
     */
    @PreAuthorize("@ss.hasPermi('module:leave:add')")
    @Log(title = "请假管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaLeave oaLeave)
    {
        try {
            return oaLeaveService.insertOaLeave(oaLeave);
        } catch (Exception e) {
            logger.error("新增请假管理失败",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改请假管理
     */
    @PreAuthorize("@ss.hasPermi('module:leave:edit')")
    @Log(title = "请假管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaLeave oaLeave)
    {
        return toAjax(oaLeaveService.updateOaLeave(oaLeave));
    }

    /**
     * 删除请假管理
     */
    @PreAuthorize("@ss.hasPermi('module:leave:remove')")
    @Log(title = "请假管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(oaLeaveService.deleteOaLeaveByIds(ids));
    }
}
